package com.example.demo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 86151 on 2018/12/25.
 */
//告诉springboot需要加载这个类
@Component
//告诉springboot这是面向切面的类
@Aspect
@Slf4j
public class DemoAop {
    @Pointcut("execution(public * com.example.demo.controller.*.*(..))")
    public void point(){
        System.out.println("test");
    }

    @Before("point()")
    public void beforeLog(){
        log.info("操作后台接口之前的日志记录");
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String method = request.getMethod();
        String url = request.getRequestURI();
        System.out.println("方法名称=="+method);
        log.info("方法名称=="+method);
        System.out.println("地址=="+url);
        log.info("地址=="+url);
    }

    @After("point()")
    public void after(){
        log.info("操作后台接口之后的日志记录");
    }
}
